<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>nodejs讲解（1） | 技术笔记</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="nodejs的介绍1 Chrome V8 引擎2 事件驱动3 非阻塞的i&#x2F;o  nodejs 也是一个JavaScript运行时  nodejs的简单使用这里举例：http模块 1234567891011121314var http &#x3D;require(&#39;http&#39;);var url &#x3D; &#39;http:&#x2F;&#x2F;www.baidu.com&amp;#3">
<meta property="og:type" content="article">
<meta property="og:title" content="nodejs讲解（1）">
<meta property="og:url" content="http://wben107.gitee.io/hexo-blog/2020/05/18/nodejs/nodejs%E8%AE%B2%E8%A7%A3%EF%BC%881%EF%BC%89/index.html">
<meta property="og:site_name" content="技术笔记">
<meta property="og:description" content="nodejs的介绍1 Chrome V8 引擎2 事件驱动3 非阻塞的i&#x2F;o  nodejs 也是一个JavaScript运行时  nodejs的简单使用这里举例：http模块 1234567891011121314var http &#x3D;require(&#39;http&#39;);var url &#x3D; &#39;http:&#x2F;&#x2F;www.baidu.com&amp;#3">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-05-18T14:04:54.000Z">
<meta property="article:modified_time" content="2020-05-22T09:18:31.566Z">
<meta property="article:author" content="biner">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/hexo-blog/atom.xml" title="技术笔记" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/hexo-blog/css/style.css">

<meta name="generator" content="Hexo 4.2.1"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/hexo-blog/" id="logo">技术笔记</a>
      </h1>
      
        <h2 id="subtitle-wrap">
          <a href="/hexo-blog/" id="subtitle">技术笔记</a>
        </h2>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/hexo-blog/">Home</a>
        
          <a class="main-nav-link" href="/hexo-blog/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/hexo-blog/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="搜索"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://wben107.gitee.io/hexo-blog"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-nodejs/nodejs讲解（1）" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/hexo-blog/2020/05/18/nodejs/nodejs%E8%AE%B2%E8%A7%A3%EF%BC%881%EF%BC%89/" class="article-date">
  <time datetime="2020-05-18T14:04:54.000Z" itemprop="datePublished">2020-05-18</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      nodejs讲解（1）
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h3 id="nodejs的介绍"><a href="#nodejs的介绍" class="headerlink" title="nodejs的介绍"></a>nodejs的介绍</h3><p>1 Chrome V8 引擎<br>2 事件驱动<br>3 非阻塞的i/o</p>
<blockquote>
<p>nodejs 也是一个JavaScript运行时</p>
</blockquote>
<h3 id="nodejs的简单使用"><a href="#nodejs的简单使用" class="headerlink" title="nodejs的简单使用"></a>nodejs的简单使用</h3><p>这里举例：http模块</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">var http &#x3D;require(&#39;http&#39;);</span><br><span class="line">var url &#x3D; &#39;http:&#x2F;&#x2F;www.baidu.com&#39;;</span><br><span class="line">var callback &#x3D; (res) &#x3D;&gt; &#123; </span><br><span class="line">var rowData &#x3D; &#39;&#39;;</span><br><span class="line">res.on(&#39;data&#39;,chuck &#x3D;&gt; rowData+&#x3D; chuck);</span><br><span class="line">res.on(&#39;end&#39;, ()&#x3D;&gt;&#123;</span><br><span class="line">    console.log(rowData.toString(&#39;utf-8&#39;));</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">&#125; </span><br><span class="line">http.get(url,callback);</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; callback是可选参数 (这里我的习惯使用回调)</span><br></pre></td></tr></table></figure>


<h3 id="创建一个简单的http服务"><a href="#创建一个简单的http服务" class="headerlink" title="创建一个简单的http服务"></a>创建一个简单的http服务</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">   <span class="keyword">var</span> http = <span class="built_in">require</span>(<span class="string">'http'</span>);</span><br><span class="line"><span class="keyword">var</span> server = http.createServer();</span><br><span class="line"><span class="keyword">const</span> users = [];</span><br><span class="line">server.on(<span class="string">'request'</span>,(request,response)=&gt;&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">var</span> url = request.url;</span><br><span class="line">    <span class="keyword">var</span> method =  request.method;</span><br><span class="line">    <span class="comment">// 创建一个路由</span></span><br><span class="line">    <span class="keyword">switch</span>(<span class="literal">true</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">case</span> url.indexOf(<span class="string">'/user'</span>) &gt;= <span class="number">0</span> :</span><br><span class="line">            <span class="keyword">switch</span>(method)&#123;</span><br><span class="line">                <span class="keyword">case</span> <span class="string">'GET'</span>:</span><br><span class="line">                    response.statusCode = <span class="number">200</span>;</span><br><span class="line">                    response.end(<span class="built_in">JSON</span>.stringify(users));</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                <span class="keyword">case</span> <span class="string">'POST'</span>:</span><br><span class="line">                    <span class="keyword">if</span>(request.headers[<span class="string">'content-type'</span>] == <span class="string">'application/json'</span>)&#123;</span><br><span class="line">                         response.statusCode = <span class="number">200</span>;</span><br><span class="line">                        <span class="keyword">var</span> chuckrow = <span class="string">''</span>;</span><br><span class="line">                        request.on(<span class="string">'data'</span>,chuck =&gt; &#123;</span><br><span class="line">                            chuckrow += chuck;</span><br><span class="line">                        &#125;);</span><br><span class="line">                        request.on(<span class="string">'end'</span>,<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">                            <span class="keyword">var</span> user = <span class="built_in">JSON</span>.parse(chuckrow.toString());</span><br><span class="line">                            users.push(user);</span><br><span class="line">                            response.end(chuckrow.toString());</span><br><span class="line">                        &#125;);</span><br><span class="line">                    &#125;</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">default</span>:</span><br><span class="line">             response.statusCode = <span class="number">404</span>;</span><br><span class="line">             response.end(<span class="string">'not found!'</span>);</span><br><span class="line">             <span class="keyword">break</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&#125;)</span><br><span class="line">server.listen(<span class="number">8888</span>);</span><br></pre></td></tr></table></figure>


      
    </div>
    <footer class="article-footer">
      <a data-url="http://wben107.gitee.io/hexo-blog/2020/05/18/nodejs/nodejs%E8%AE%B2%E8%A7%A3%EF%BC%881%EF%BC%89/" data-id="ckak98k5c00093ms6fex218me" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/hexo-blog/2020/05/23/nodejs/nodejs%E7%9A%84mvc(2)/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          nodejs的mvc
        
      </div>
    </a>
  
  
    <a href="/hexo-blog/2020/05/17/%E5%88%9D%E6%AC%A1%E6%8E%A5%E8%A7%A6hexo-%E5%AD%A6%E4%B9%A0%E6%AD%A5%E9%AA%A4/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">初次接触hexo --学习步骤</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">分类</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/hexo-blog/categories/coding/">coding</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">标签</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/hexo-blog/tags/javascript/" rel="tag">javascript</a></li><li class="tag-list-item"><a class="tag-list-link" href="/hexo-blog/tags/nodejs/" rel="tag">nodejs</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">标签云</h3>
    <div class="widget tagcloud">
      <a href="/hexo-blog/tags/javascript/" style="font-size: 10px;">javascript</a> <a href="/hexo-blog/tags/nodejs/" style="font-size: 10px;">nodejs</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">归档</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/hexo-blog/archives/2020/05/">五月 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/hexo-blog/2020/05/24/nodejs/%E5%AD%A6%E4%B9%A0mongodb%E7%9A%84%E5%9F%BA%E7%A1%80%E7%94%A8%E6%B3%95%20%E5%A2%9E%E5%88%A0%E6%94%B9%E6%9F%A5+%E8%81%9A%E5%90%88%E6%9F%A5%E8%AF%A2/">学习mongodb的基础用法 增删改查 + 聚合查询</a>
          </li>
        
          <li>
            <a href="/hexo-blog/2020/05/23/nodejs/nodejs%E7%9A%84mvc(2)/">nodejs的mvc</a>
          </li>
        
          <li>
            <a href="/hexo-blog/2020/05/18/nodejs/nodejs%E8%AE%B2%E8%A7%A3%EF%BC%881%EF%BC%89/">nodejs讲解（1）</a>
          </li>
        
          <li>
            <a href="/hexo-blog/2020/05/17/%E5%88%9D%E6%AC%A1%E6%8E%A5%E8%A7%A6hexo-%E5%AD%A6%E4%B9%A0%E6%AD%A5%E9%AA%A4/">初次接触hexo --学习步骤</a>
          </li>
        
          <li>
            <a href="/hexo-blog/2020/05/17/my-first-article/">my first article</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2020 biner<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/hexo-blog/" class="mobile-nav-link">Home</a>
  
    <a href="/hexo-blog/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="/hexo-blog/fancybox/jquery.fancybox.css">

  
<script src="/hexo-blog/fancybox/jquery.fancybox.pack.js"></script>




<script src="/hexo-blog/js/script.js"></script>




  </div>
</body>
</html>